package com.jzqf.wheel;

import android.content.Context;
import android.util.DisplayMetrics;
import android.util.TypedValue;

public class DensityUtil {
    /**
     * 获取屏幕分辨率-单位px
     *
     * @param context
     * @return
     */
    public static int[] getScreenSizePx(Context context) {
        int[] screenPx = new int[2];
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        screenPx[0] = dm.widthPixels;
        screenPx[1] = dm.heightPixels;
        return screenPx;
    }

    /**
     * 获取屏幕分辨率-单位dpi
     *
     * @param context
     * @return
     */
    public static int[] getScreenSizeDpi(Context context) {
        int[] screenDpi = new int[3];
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        screenDpi[0] = (int) (dm.widthPixels / dm.density);
        screenDpi[1] = (int) (dm.heightPixels / dm.density);
        screenDpi[2] = dm.densityDpi;
        return screenDpi;
    }
    /**
     * @param dpValue 需要转换的dp值
     * @return 转换后的px值
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        //还可以通过如下代码获得
        //Resources.getSystem().getDisplayMetrics().density
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * dp转px
     *
     * @param context
     * @return
     */
    public static int dp2px(Context context, float dpVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, context.getResources().getDisplayMetrics());
    }


    /**
     * sp转px
     *
     * @param context
     * @return
     */
    public static int sp2px(Context context, float spVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                spVal, context.getResources().getDisplayMetrics());
    }


    /**
     * px转dp
     *
     * @param context
     * @param pxVal
     * @return
     */
    public static float px2dp(Context context, float pxVal) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (pxVal / scale);
    }


    /**
     * px转sp
     *
     * @param pxVal
     * @return
     */
    public static float px2sp(Context context, float pxVal) {
        return (pxVal / context.getResources().getDisplayMetrics().scaledDensity);
    }


    public static float convertDpToPixel(Context context, float dp) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        return dp * metrics.density;
    }
}
